package com.smyh.model.host.param;

import android.content.Context;
import android.graphics.Color;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.smyh.UIM.host.param.Param_101Activity;
import com.smyh.bean.Protocol101SetInfo;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.model.tableModel.KtableView;
import com.smyh.protcol.host.HostCMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by 姜龙 on 2016/7/8.
 */
public class Param101Model {

    private static TableLayout tableLayout;

    public static TableLayout getTableLayout(Context context){
        if(tableLayout == null){
            tableLayout = new TableLayout(context);
            tableLayout.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tableLayout.setBackgroundColor(Color.BLACK);
            KtableView.setIsEditColNum(1);
            String header0[] = {"101参数"};
            String header1[] = {"名称","值","描述"};
            String test1[] = {"链路地址","","2字节"};
            String test2[] = {"链路地址长度","","1字节"};
            String test3[] = {"传输原因长度","","1字节"};
            String test4[] = {"可变结构限定词长度","","1字节"};
            String test5[] = {"信息体地址长度","","1字节"};
            String test6[] = {"超时时间","","1字节"};
            String test7[] = {"重发次数","","1字节"};
            String test8[] = {"主机编码","","2个16进制字节"};
            tableLayout.addView(KtableView.addTableHeaders(context,header0));
            tableLayout.addView(KtableView.addTableHeaders(context,header1));
            tableLayout.addView(KtableView.addNewRow(context,test1));
            tableLayout.addView(KtableView.addNewRow(context,test2));
            tableLayout.addView(KtableView.addNewRow(context,test3));
            tableLayout.addView(KtableView.addNewRow(context,test4));
            tableLayout.addView(KtableView.addNewRow(context,test5));
            tableLayout.addView(KtableView.addNewRow(context,test6));
            tableLayout.addView(KtableView.addNewRow(context,test7));
            tableLayout.addView(KtableView.addNewRow(context,test8));
        }
        return tableLayout;
    }

    public static IoBuffer writeParam(){
        //101参数修改
        IoBuffer buf = IoBuffer.allocate(0).setAutoExpand(true);
        for (int j = 2; j < tableLayout.getChildCount(); j++) {
            TableRow childRow = (TableRow) tableLayout.getChildAt(j);
            switch (j) {
                case 2:{
                    String linkAddrData = ((TextView) childRow.getChildAt(1)).getText().toString();
                    linkAddrData = linkAddrData.replace(" ", "");
//                    int cell = Integer.parseInt(""+(linkAddrData.equals("")?0:linkAddrData),16) ;
                    byte[] bTemp = HexStrUtil.hexStr2Bytes(linkAddrData);
//                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[2];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 3:{
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 4:{
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 5:{
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
                case 6:{
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }

                case 7:{
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }

                case 8:{
                    int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(Integer.toHexString(cell));
                    byte[] bt = new byte[1];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }

                case 9:{
                    String hostCodeBakStr = ((TextView) childRow.getChildAt(1)).getText().toString().replaceAll(" ", "");
                    int cell = Integer.parseInt(""+(hostCodeBakStr.equals("")?0:hostCodeBakStr),16) ;
                    byte[] bTemp =  HexStrUtil.hexStr2Bytes(String.format("%04X",cell));
                    byte[] bt = new byte[2];
                    for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                        bt[i] = bTemp[i];
                    }
                    buf.put(bt);
                    break;
                }
            }
        }
        byte[] bTemp = new byte[10];
        buf.put(bTemp);
        buf.flip();
        IoBuffer buf1 = HostCMDUtil.writeParam(1, buf,(byte)0);
        return buf1;
    }

    public static void setTableLayout() {

        Protocol101SetInfo info = Param_101Activity.param101Map.get(MainCahce.chooseDeviceAddr);
        for (int i = 2; i < tableLayout.getChildCount(); i++) {
            TableRow childRow = (TableRow) tableLayout.getChildAt(i);

            switch (i) {
                case 2:
                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(""+info.getLinkAddr());
                    }else {
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                    }
                    break;
                case 3:
                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(""+info.getAddrLength());
                    }else {
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                    }
                    break;
                case 4:
                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(""+info.getTransCauseLength());
                    }else {
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                    }
                    break;
                case 5:
                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(""+info.getVsqLength());
                    }else {
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                    }
                    break;
                case 6:
                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(""+info.getInfoObjLength());
                    }else {
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                    }
                    break;
                case 7:
                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(""+info.getOverTimes());
                    }else {
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                    }
                    break;
                case 8:
                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(""+info.getRepeatTimes());
                    }else {
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                    }
                    break;
                case 9:
                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(""+info.getHostCode());
                    }else {
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                    }
                    break;
            }
        }
    }
}
